package com.miage.m1.xml;

import java.util.*;

import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import com.miage.m1.xml.Player;

public class Club {
	
	private int id;
	private String name;
	private int establishment;
	private Person president;
	private Person coach;
	private List<Player> players;
	
	protected AttributesImpl atts = new AttributesImpl();
	
	/* Constructors */
	/**
	 * 
	 * @param id
	 * @param name
	 * @param establishment
	 * @param president
	 * @param coach
	 * @param players
	 */
	public Club(int id, String name, int establishment, Person president, Person coach, List<Player> players){
		this.id = id;
		this.name = name;
		this.establishment = establishment;
		this.president = president;
		this.coach = coach;
		this.players = players;
		}
	
	/* Getter and Setter */
	public int getId(){ return this.id; }
	
	public String getName(){ return this.name; }
	
	public int getEstablishment(){ return this.establishment; }
	
	public List<Player> getPlayers() { return this.players; }
	protected void setPlayers(List<Player> players) { this.players = players; }
	
	public Person getPresident(){ return this.president; }
	protected void setPresident(Person president){ this.president = president; }
	
	public Person getCoach() { return this.coach; }
	
	/* Methods and Properties */
	
	public ContentHandler Serialize(ContentHandler handler) throws SAXException{
		//TODO Create the serialization of a Player.
		if(handler == null){
			throw new SAXException("Pas de ContentHandler");
		}
		
		atts.addAttribute("","id","id","",String.valueOf(this.getId()));
		atts.addAttribute("", "establishment", "establishment", "", String.valueOf(this.getEstablishment()));
		handler.startElement("", "club", "club", atts);
		atts.clear();
		
		handler.startElement("", "name", "name", atts);
		char[] name = this.getName().toCharArray();
		handler.characters(name,0,name.length);
		handler.endElement("", "name", "name");
		
		handler.startElement("", "president", "president", atts);
		this.getPresident().Serialize(handler);
		handler.endElement("", "president", "president");
		
		handler.startElement("", "coach", "coach", atts);
		this.getCoach().Serialize(handler);
		handler.endElement("", "coach", "coach");
		
		handler.startElement("", "players", "players", atts);
		
		for(Player p : this.getPlayers()){
			
	
			p.Serialize(handler);
			
			
		}
		
		handler.endElement("", "players", "players");
		
		handler.endElement("", "club", "club");
		
		return handler;
	}

}
